<?php
namespace application\admin\panels;

class user_forms extends panel {
	public $name = 'User Forms';
	
	
	public function handleData() {
		if($_REQUEST['update_fields']) {
			DB()->query("UPDATE `cn_content_fields` SET `cf_show_signup`='0', `cf_show_edit_profile`='0'");

			if(is_array($_REQUEST['show_signups'])) {
				$string = "'".implode("', '", $_REQUEST['show_signups'])."'";
				DB()->query("UPDATE `cn_content_fields` SET `cf_show_signup`='1' WHERE `cf_id` IN ($string)");
			}
			if(is_array($_REQUEST['show_profiles'])) {
				$string = "'".implode("', '", $_REQUEST['show_profiles'])."'";
				DB()->query("UPDATE `cn_content_fields` SET `cf_show_edit_profile`='1' WHERE `cf_id` IN ($string)");
			}
			$this->addMessage('success', 'Fields updated successfully.');
		}
	}
	
	public function display() {
		global $concerto;
		$concerto->UsersQuery->initMainQuery();
		
		$columns = DB()->getResults("SELECT * FROM `cn_content_fields` WHERE `cf_content_type`='0'");
		
		$this->displayMessages();

		
		?>
		
		<div class="box">
			<h3>Which Fields show on Which Forms?</h3>
			<form method="post">
			<table class="admin-table" width="100%">
				<thead>
					<tr>
						<th class="first" width="2%"></th>
						<th>Field Name</th>
						<th>Signup</th>
						<th>Edit Profile</th>
						<th class="last" width="2%"></th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<th class="first" width="2%"></th>
						<th>Field Name</th>
						<th>Signup</th>
						<th>Edit Profile</th>
						<th class="last" width="2%"></th>
					</tr>
				</tfoot>
				
				<tbody>
					
					<?php $style = ''; ?>
					<?php $innerStyle = '';?>
					<?php foreach($columns as $column) : ?>
					
					<tr<?=$style?>>
						<td class="first"></td>	
						<td><?=$column->cf_display_name?></td>		
						<td><input name="show_signups[]" type="checkbox" value="<?=$column->cf_id?>" <?=($column->cf_show_signup) ? 'checked="checked"' : ''?>/></td>
						<td><input name="show_profiles[]" type="checkbox" value="<?=$column->cf_id?>" <?=($column->cf_show_edit_profile) ? 'checked="checked"' : ''?>/></td>
						<td class="last"></td>
					</tr>
					
					<?php $style = ($style == ' class="alternate"') ? '' : ' class="alternate"';?>
					<?php endforeach;?>
				</tbody>
			</table>
			
		</div>
		<input type="submit" class="big-button" value="Submit" name="update_fields"/>
			</form>
		<?php
	}
}
?>